package com.qf.day09.work1;

import java.util.Arrays;

public class UserServiceImpl implements  UserService{
    private User[] users = new User[5];
    private int count = 0;
    public UserServiceImpl() {
    }

    public UserServiceImpl(User[] users) {
        this.users = users;
    }

    public User[] getUsers() {
        return users;
    }

    public void setUsers(User[] users) {
        this.users = users;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public Boolean login(String name, String password) {
        for (int i = 0; i < count; i++) {
            User user = users[i];
            if(name.equals(user.getUserName())&&password.equals(user.getPassword())){
                return true;
            }
        }
        return false;
    }

    @Override
    public Boolean register(String name, String password) {
        if(checkUserName(name)){
            //用户名重复啦
            return false;
        }
        //创建user存入数组
        User user = new User(name,password);
        this.users[this.count++] = user;
        //数组满了要扩容
        if(count == users.length){
            users = Arrays.copyOf(users , users.length*2);
        }
        return true;
    }
    private Boolean checkUserName(String name) {
        for (int i = 0; i < this.count; i++) {
            User user = users[i];
            if(user.getUserName().equals(name)){
                return true;
            }
        }
        return false;
    }
}
